package gases.as3.librarys.components.ui.control.datagrid
{
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.utils.Dictionary;
	
	import gases.as3.librarys.components.events.GDataGridEvent;
	import gases.as3.librarys.components.ui.GUIComponents;
	
	public class GDataGrid extends GUIComponents
	{
		public function GDataGrid()
		{
			Mouse.cursor = "arrow";
			super();
		}
		
		override protected function initialization():void
		{
			lineArr = new Array();
		}
		
		override public function dispose():void
		{
		}
		
		/**
		 *设置数据的标题栏  
		 * @param dataArr
		 * @param colorArr
		 * @param widthArr
		 * @param height
		 * 
		 */		
		public function setDataHead(dataArr:Array,colorArr:Array = null,widthArr:Array = null,height:Number = 20):void
		{
			if(_head)
			{	
				if(this.contains(_head))
				{
					this.removeChild(_head);
				}
				_head.dispose();
				_head = null;
			}
			_head = new GridLine(dataArr,colorArr,widthArr,height);
			this.addChild(_head);
			
			_width = _head.width;
			_headNumber = dataArr.length;
		}
		
		/**
		 *设置datagrid的 
		 * @param dataArrs
		 * @param colorArrs
		 * @param widthArrs
		 * @param heightArr
		 * @param listenersArr 是否监听鼠标事件
		 * 
		 */		
		public function dataProvider(dataArrs:Array,colorArrs:Array = null,widthArrs:Array = null,heightArr:Array = null,listenersArr:Array = null):void
		{
			clearData();
			if(dataArrs == null)
			{
				return;
			}
			if(dataArrs.length == 0)
			{
				return;
			}
			_dataArrs = dataArrs;
			_colorArrs = fillArray(colorArrs,dataArrs);
			_widthArrs = fillArray(widthArrs,dataArrs);
			_heightArr = fillArray(heightArr,dataArrs,20);
			_listenersArr = fillArray(listenersArr,dataArrs,true);
			for(var i:int = 0;i<_dataArrs.length;i++)
			{
				var line:GridLine =  new GridLine(_dataArrs[i],_colorArrs[i],_widthArrs[i],_heightArr[i],_listenersArr[i]);
				line.y = _height;
				_height += line.height;
				this.addChild(line);
				lineArr[i] = line;
			}
			addDataGridListeners();
		}
		
		private function addDataGridListeners():void
		{
			if(lineArr == null)
			{
				return;	
			}
			for ( var i:int=0;i<lineArr.length;i++)
			{ 	
				var line:GridLine = lineArr[i] as GridLine;
				line.addEventListener(MouseEvent.MOUSE_OVER,itemOver);
				line.addEventListener(MouseEvent.MOUSE_OUT,itemOut);
				line.addEventListener(MouseEvent.CLICK,itemClick);
			}
		}
		
		private function itemOver(e:MouseEvent):void
		{
			var line:GridLine = e.currentTarget as GridLine;
			line.lineOver();
		}
		
		private function itemOut(e:MouseEvent):void
		{
			var line:GridLine = e.currentTarget as GridLine;
			line.lineOut();
		}
		
		private function itemClick(e:MouseEvent):void
		{
			var line:GridLine = e.currentTarget as GridLine;
			var index:int;
			for(var i:int=0;i<lineArr.length;i++)
			{
				var obj:GridLine = lineArr[i] as GridLine;
				if(obj != line)
				{
					obj.addEventListener(MouseEvent.MOUSE_OVER,itemOver);
					obj.addEventListener(MouseEvent.MOUSE_OUT,itemOut);
					obj.addEventListener(MouseEvent.CLICK,itemClick);
					obj.lineOut();
				}
				else
				{
					line.removeEventListener(MouseEvent.MOUSE_OUT,itemOut);
					line.removeEventListener(MouseEvent.MOUSE_OVER,itemOver);
					line.lineClick();
					index = i;
				}
			}
			this.dispatchEvent(new GDataGridEvent(GDataGridEvent.ITEM_CLICK,line.dataArray,index));
		}
		
		private function clearData():void
		{
			_dataArrs = null;
			_colorArrs = null;
			_widthArrs = null;
			_heightArr = null;
			_listenersArr = null;
			_height = _head.height;;
			for(var i:int = 0;i<lineArr.length;i++)
			{
				var line:GridLine = lineArr[i] as GridLine;
				if(this.contains(line))
				{
					this.removeChild(line);
				}
				line.dispose();
				line = null;
			}
			lineArr = new Array();
		}
		
		/**
		 *补充数值 
		 * @param tarArr
		 * @param compArr
		 * @return 
		 * 
		 */		
		private function fillArray(tarArr:Array,compArr:Array,value:* = null):Array
		{
			if(tarArr == null)
			{
				tarArr = new Array();
			}
			var les:int = compArr.length - tarArr.length;
			if(les >0)
			{
				for(var i:int = 0;i<les;i++)
				{
					tarArr.push(value);
				}
			}
			return tarArr;
		}
		
		private var _dataArrs:Array;
		private var _colorArrs:Array;
		private var _widthArrs:Array;
		private var _heightArr:Array;
		private var _listenersArr:Array;
		
		private var _head:GridLine;
		private var _headNumber:int;
		/**
		 *当前datagrid的长度 
		 */		
		private var _width:Number;
		private var _height:Number;
		
		public var lineArr:Array;
		
	}
}